package com.ricky.service_hospital.controller.api;

import com.ricky.common_util.result.Result;
import com.ricky.common_util.result.ResultCodeEnum;
import com.ricky.hosp.model.hosp.Department;
import com.ricky.hosp.model.hosp.Hospital;
import com.ricky.hosp.model.hosp.Schedule;
import com.ricky.hosp.vo.hosp.DepartmentQueryVo;
import com.ricky.hosp.vo.hosp.ScheduleQueryVo;
import com.ricky.service_hospital.service.DepartmentService;
import com.ricky.service_hospital.service.HospitalService;
import com.ricky.service_hospital.service.HospitalSetService;
import com.ricky.service_hospital.service.ScheduleService;
import com.ricky.service_hospital.utils.SignKeyVerification;
import com.ricky.service_util.common.exception.ReservationException;
import com.ricky.service_util.common.helper.HttpRequestHelper;
import com.ricky.service_util.common.utils.MD5;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

//提供给外部客户端调用的API
@Api(tags = "医院客户端操作接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private SignKeyVerification signKeyVerification;
    //查询医院接口
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取医院传递过来的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取传递过来的医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取医院客户端传递过来的签名
        String hospSign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //数据库查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        if(!hospSign.equals(signKeyMd5)){
            throw new ReservationException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法根据医院编号查询信息
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }
    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取医院传递过来的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院客户端传递过来的签名
        String hospSign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String hoscode = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //数据库查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(signKeyMd5)){
            throw new ReservationException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中base64格式图片中的“+”会被替换为“ ”，在存入数据库之前必须对数据做还原
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);
        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }
    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取医院传递过来的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院客户端传递过来的签名
        String hospSign = (String) paramMap.get("sign");
        //根据传递过来的医院编号，查询数据库，查询签名
        String hoscode = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //数据库查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(signKeyMd5)){
            throw new ReservationException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();
    }
    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取医院传递过来的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //当前页和每页的记录数
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
        //签名校验
        String hospSign = (String) paramMap.get("sign");
        if(!signKeyVerification.verifyMD5(hoscode, hospSign)) {
            throw new ReservationException(ResultCodeEnum.SIGN_ERROR);
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }
    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取医院传递过来的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号 + 科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //校验签名
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //数据库查询出来的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(signKeyMd5)){
            throw new ReservationException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }
    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        //校验签名
        if(!signKeyVerification.verifyMD5(hoscode,sign)){
            throw new ReservationException(ResultCodeEnum.SIGN_ERROR);
        }
        //当前页和每页的记录数
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
        scheduleService.save(paramMap);
        return Result.ok();
    }
    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String sign = (String) paramMap.get("sign");
        if(!signKeyVerification.verifyMD5(hoscode,sign)){
            throw new ReservationException(ResultCodeEnum.SIGN_ERROR);
        }
        //当前页和每页的记录数
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }
    //删除排班接口
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        String sign = (String) paramMap.get("sign");
        if(!signKeyVerification.verifyMD5(hoscode,sign)){
            throw new ReservationException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
}
